package com.xsprite.managesys.listener;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.game.GMClientServer;
import com.game.gm.load.GMCommLoad;
import com.xsprite.managesys.bean.XlsxFileBean;
import com.xsprite.managesys.log.LogManager;
import com.xsprite.managesys.service.ServersDatabaseManager;
import com.xsprite.managesys.service.ServersManager;
import com.xsprite.managesys.service.XlsxFileManager;
import com.zhangzhang.util.Util;

public class MyContextListener implements ServletContextListener {

	private ServletContext context = null;

	public void contextDestroyed(ServletContextEvent event) {
		this.context = null;
		XlsxFileManager.getInstance().clearBeans();
	}

	public void contextInitialized(ServletContextEvent event) {
		try{
			context = event.getServletContext();
			loadExistsFiles();
			loadItemExcel();
			loadServers();
			loadQueryEmailServers();
			loadGMCommands();
			new Thread(GMClientServer.getInstance()).start();
		} catch (Exception ex){
			ex.printStackTrace();
			System.out.println("初始化监听器失败");
		}
	}
	
	private void loadGMCommands(){
		GMCommLoad.getInstance();
	}
	
	private void loadItemExcel() throws Exception{
		String basePath = this.context.getRealPath("/config/items.xlsm");
		XlsxFileBean itemsBean = new XlsxFileBean();
		XlsxFileManager.getInstance().setItemsBean(itemsBean);
		XlsxFileManager.getInstance().readItemExcelFile(basePath, itemsBean);
		XlsxFileManager.getInstance().loadItemsMap();
	}

	private void loadExistsFiles() throws IOException {
		String basePath = this.context.getRealPath("/upload/");
		if(!Util.hasDirectory(basePath)){
			boolean isCreateSuccess = Util.createDirectory(basePath);
			System.out.println("是否创建了upload" + isCreateSuccess);
		}
		File dir = new File(basePath);
		File[] files = dir.listFiles(new FileFilter() {
			public boolean accept(File f) {
				if (f.getName().endsWith(".xlsx")) {
					return true;
				}
				return false;
			}
		});
		if(files != null){
			for (File f : files) {
				XlsxFileBean bean = new XlsxFileBean();
				bean.setFilename(f.getName());
				bean.setFileSize(f.length());
				bean.setUploadDate(f.lastModified());
				bean.setFilePath(basePath + "\\" + f.getName()); 
				XlsxFileManager.getInstance().addBean(bean);
			}
		}
		try{
			XlsxFileManager.getInstance().reloadExcelDatas();
		} catch (Exception ex){
			LogManager.getInstance().log4j(ex.getMessage());
		}
	}
	
	private void loadQueryEmailServers(){
		String clientServerPath = this.context.getRealPath("/config/clientServer.xml");
		String dblistPath = this.context.getRealPath("/config/dblist.xml");
		String batisPath = this.context.getRealPath("/config/mybatis-config.xml");
		try{
			ServersDatabaseManager.getInstance().loadServers(clientServerPath, dblistPath, batisPath);
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}

	private void loadServers() {
		String basePath = this.context.getRealPath("/config/dataserver.xml");
		System.out.println("basePath=" + basePath);
		try {
			ServersManager.getInstance().loadServers(basePath);
		} catch (Exception ex) {
			LogManager.getInstance().log4j(ex.getMessage());
		}
	}
}
